package com.yckj.activechicken.business.system.controller;

import com.yckj.activechicken.business.system.model.SysCompany;
import com.yckj.activechicken.business.system.service.ISysCompanyService;
import com.yckj.activechicken.common.utils.security.SecurityUtils;
import com.yckj.activechicken.framework.web.controller.BaseController;
import com.yckj.activechicken.framework.web.domain.AjaxResult;
import com.yckj.activechicken.framework.web.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * Title:com.yckj.activechicken.business.controller
 * </p>
 * <p/>
 * <p>
 * Description: 描述【公司信息管理控制器】
 * </p>
 * <p/>
 * <p>
 * Copyright: Copyright (c) 2019
 * </p>
 * <p/>
 * <p>
 * Company: 英才汇硕信息技术有限公司
 * </p>
 *
 * @author zhoujy
 * @version 1.0
 * @created 2020/5/13 17:36
 */
@RestController
@RequestMapping("/organization/company")
public class SysCompanyController extends BaseController {
    @Autowired
    private ISysCompanyService companyService;

    /**
     * 添加公司信息
     */
    @PreAuthorize("@ss.hasPermi('organization:company:addCompany')")
    @PostMapping("/addCompany")
    public AjaxResult addCompany(SysCompany sysCompany) {
        String username = SecurityUtils.getUsername();
        sysCompany.setCreateBy(username);
        Date newDate = new Date();
        sysCompany.setCreateTime(newDate);
        sysCompany.setUpdateBy(username);
        sysCompany.setUpdateTime(newDate);
        Map<String,String> outMap = companyService.addCompany(sysCompany);
        String result = outMap.get("result");
        if("true".equals(result)){
            return AjaxResult.success();
        }else{
            return AjaxResult.error(outMap.get("message"));
        }
    }

    @PreAuthorize("@ss.hasPermi('organization:company:modifyCompany')")
    @PostMapping("/modifyCompany")
    public AjaxResult modifyCompany(SysCompany sysCompany) {
        sysCompany.setUpdateBy(SecurityUtils.getUsername());
        sysCompany.setUpdateTime(new Date());
        Map<String,String> outMap = companyService.modfiyCompany(sysCompany);
        String result = outMap.get("result");
        if("true".equals(result)){
            return AjaxResult.success();
        }else{
            return AjaxResult.error(outMap.get("message"));
        }
    }

    /**
     * 多条件分页查询公司信息
     * @param sysCompany
     * @return
     */
    @PreAuthorize("@ss.hasPermi('organization:company:listCompany')")
    @GetMapping("/listCompany")
    public TableDataInfo listCompany(SysCompany sysCompany) {
        startPage();
        List<SysCompany> list = companyService.listCompany(sysCompany);
        return getDataTable(list);
    }

    /**
     * 删除公司信息，接触部门关系
     * @param sysCompany
     * @return
     */
    @PreAuthorize("@ss.hasPermi('organization:company:delCompany')")
    @PostMapping("/delCompany")
    public AjaxResult delCompany(@RequestBody SysCompany sysCompany) {

        if("系统公司".equals(sysCompany.getType())){
            return AjaxResult.error("不允许删除系统公司！");
        }
        String username = SecurityUtils.getUsername();
        Date newDate = new Date();
        sysCompany.setUpdateBy(username);
        sysCompany.setUpdateTime(newDate);
        return toAjax(companyService.delCompany(sysCompany));
    }
}
